﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RestSharp; //see Restsharp.org
using RestSharp.Authenticators; //see Restsharp.org
using System.Security.Cryptography; //used for MD5 hashing
using System.Web;

namespace BoxcarProviderClient
{
    public partial class TesterUI : Form
    {
        public TesterUI()
        {
            InitializeComponent();
        }
        //subscribe this user to your service
        private void btnSubscribe_Click(object sender, EventArgs e)
        {
            if (txtBoxAPI_KEY.Text != "" && txtBoxSubscriberEmail.Text !="")
            {
                responseLabel.Text = "Executing...";
                Refresh();
                RestResponse response = new RestResponse();
                BoxcarNotifier boxy = new BoxcarNotifier(txtBoxAPI_KEY.Text,null,txtBoxSubscriberEmail.Text,null,null);
                response = boxy.notifySubscriber();
                responseLabel.Text = response.StatusDescription;
            }
            else
            {
                MessageBox.Show("You must provide a valid API_KEY and subscriber email address to subscribe this user");
            }      
        }

        //notify individual subscriber
        private void btnNotifyIndividual_Click(object sender, EventArgs e)
        {
            if (txtBoxAPI_KEY.Text != "" && txtBoxIndividualEmail.Text != "")
            {
                responseLabel.Text = "Executing...";
                Refresh();
                RestResponse response = new RestResponse();
                BoxcarNotifier boxy = new BoxcarNotifier(txtBoxAPI_KEY.Text,null,txtBoxIndividualEmail.Text,null,txtBoxIndividualMsg.Text);
                response = boxy.notifyIndividual();
                responseLabel.Text = response.StatusDescription;
            }
            else
            {
                MessageBox.Show("You must provide a valid API_KEY and subscriber email address to notify an individual user");
            }
        }

        //broadcast a message to *ALL* subscribers
        private void btnBroadcast_Click(object sender, EventArgs e)
        {
            responseLabel.Text = "Executing...";
            Refresh();
            RestResponse response = new RestResponse();
            BoxcarNotifier boxy = new BoxcarNotifier(txtBoxAPI_KEY.Text,txtBoxAPI_SECRET.Text,null,null,txtBoxBroadcastMessage.Text);
            response = boxy.notifyBroadcast();
            responseLabel.Text = response.StatusDescription;
        }

        //send a personal message to yourself - without going through a provider
        private void btnSendPersonal_Click(object sender, EventArgs e)
        {
            if (txtBoxUserEmail.Text != "" && txtBoxUserPass.Text != "")
            {
                responseLabel.Text = "Executing...";
                Refresh();
                RestResponse response = new RestResponse();
                BoxcarNotifier boxy = new BoxcarNotifier(null,null,txtBoxUserEmail.Text,txtBoxUserPass.Text,txtBoxPersonalMsg.Text);
                response = boxy.notifySelf();
                responseLabel.Text = response.StatusDescription;
            }
            else
            {
                MessageBox.Show("You must provide a valid email address and password to send yourself a message");
            }
        }

        private void tabSubscribe_Enter(object sender, EventArgs e)
        {
            responseLabel.Text = "";
        }

        private void tabIndividual_Enter(object sender, EventArgs e)
        {
            responseLabel.Text = "";
        }

        private void tabBroadcast_Enter(object sender, EventArgs e)
        {
            responseLabel.Text = "";
        }

        private void tabPersonal_Enter(object sender, EventArgs e)
        {
            responseLabel.Text = "";
        }

        private void toolStripAbout_Click(object sender, EventArgs e)
        {
            MessageBox.Show("API Test Tool for Boxcar\n\nWritten by Dennis Hills\nTwitter: @dmennis\nEmail: dmennis@gmail.com", "About",
            MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
        }
    }
}
